<?php

	include 'sendquery_db.php';
	
	function error($message = "", $url = "", $url2 = "") {
		if (!$message && !$url)
			return;
	
		$message = eregi_replace("<br>|<br/>|<br />", "\\n", $message);
		$message = str_replace("\"", "\\\"", $message);
		if ($url == "self.close") {
			echo "<script language=\"JavaScript\">\n";
			if ($message) {
				echo "alert('{$message}');\n";
			}
			echo "self.close();\n";
			if ($url2) {
	
				echo "opener.location.href = \"{$url2}\";\n";
			}
			echo "</script>\n";
		} else if ($url == "history.back") {
			echo "<script language=\"JavaScript\">\n";
			if ($message) {
				echo "alert('{$message}');\n";
			}
			echo "history.back();\n";
			if ($url2) {
				echo "opener.location.href = \"{$url2}\";\n";
			}
			echo "</script>\n";
		} else {
			if (!$sented = headers_sent())
				header("Refresh: 0; URL={$url}");
			if ($message) {
				echo "<script language=\"JavaScript\">\n";
				echo "alert('{$message}');\n";
				if ($sented)
					echo "window.location.href=\"{$url}\";\n";
				echo "</script>\n";
			}
		}
	}
	
	$ul1 = "../page/join.php";
	
	$db = dbservice::getInstance();
	
	if (isset($_POST["id"])) {
		$id = $_POST["id"];
		if (!strcmp($id, "")) {
	
			$msg = "pleases type id";
			error($msg, $ul1, $ul1);
		}
	
		$query = "select id from user where id='$id'";
		$result = $db -> sendquery($query);
	
		if ($result) {
			foreach ($result as $output) {
				if (!strcmp($id, $output['id'])) {
					$msg = "id is duplication";
					error($msg, $ul1, $ul1);
				}
			}
	
		}
	}
	if (isset($_POST["psw"])) {
		$psw = $_POST["psw"];
		if (!strcmp($psw, "")) {
	
			$msg = "pleases type password";
			error($msg, $ul1, $ul1);
		}
	
	}
	if (isset($_POST["psw_check"])) {
		$psw1 = $_POST["psw_check"];
		if (!strcmp($psw1, "")) {
	
			$msg = "pleases type password";
			error($msg, $ul1, $ul1);
		}
	
		if (strcmp($psw, $psw1)) {
			$msg = "password is not equal";
			error($msg, $ul1, $ul1);
		}
	}
	if (isset($_POST["nickName"])) {
		$nickname = $_POST["nickName"];
		if (!strcmp($nickname, "")) {
	
			$msg = "pleases type username";
			error($msg, $ul1, $ul1);
		}
	
	}
	if (isset($_POST["email"])) {
		$email = $_POST["email"];
		if (!preg_match("/^[a-zA-Z_\-]+@(([a-zA-Z_\-])+\.)+[a-zA-Z]{2,4}$/", $email)) {
			$msg = "email is error";
			error($msg, $ul1, $ul1);
		}
		if (!strcmp($email, "")) {
	
			$msg = "pleases type email";
			error($msg, $ul1, $ul1);
		}
	}
	
	$save_dir = "../userpicture";
	if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) {
	
		$dest = $save_dir . "/" . $_FILES["avatar"]["name"];
	
		move_uploaded_file($_FILES["avatar"]["tmp_name"], $dest);
	
		//print "Saved uploaded file as $dest\n";
	} else {
		$msg = "pleases upload your picture";
		error($msg, $ul1, $ul1);
		exit ;
	}
	
	$query = "insert into user values('$id','$psw','$email','$nickname','$dest')";
	$result = $db -> sendquery($query);
	
	if ($result) {
		$url2 = "../page/selab_main.php";
		/*$msg = "login success";
		 error($msg,$url2,$url2);
		 */
		header('Location:' . $url2);
	} else {
		echo "fail";
	
	}
?>

